<?php
declare (strict_types = 1);

namespace app\city24\controller;

use app\BaseController;
use think\facade\Env;
use think\facade\Db;
use app\nizw\Help;
use think\facade\Log;




class Index extends BaseController
{
    public function index()
    {
        $data=['game'=>'city24'
        ];
        return json($data);
    }
    public function login($openId,$name="",$avater="")
    {
        $whereTable = [
            'openId'=>['eq',$openId]
        ];
        $u = DB::connect("mysql_city24")->name("user");
        $w = $u->where($whereTable);
        $user = $w->limit(1)->select();
        $res=['code'=>200,'time'=>time()];

        if(count($user)==0){
            $data =[
                'openId'=> $openId,
                'name'=> $name,
                'avater'=> $avater,
                'time'=> time(),
                'update'=> 0,
            ];
            $id = $u->insertGetId($data);
            $res["userId"]=$id;
        }else{
            $data =[];
            $change =false;
            if(!empty($name) && $user[0]["name"]!=$name){
                $data["name"]=$name;
                $change=true;
            }
            if(!empty($avater) && $user[0]["avater"]!=$avater){
                $data["avater"]=$avater;
                $change=true;
            }
            if($change == true){
                $w->update($data);
            }
            $res["userId"]=$user[0]["id"];
        }

        return json($res);
    }

    public function getdata($openId){
        $whereTable = [
            'openId'=>['eq',$openId]
        ];
        $u = DB::connect("mysql_city24")->name("user");
        $w = $u->where($whereTable);
        $user = $w->limit(1)->select();
        if(count($user)==0){
            $res=[
                "code"=>0,
                "user"=>"{}"
            ];
            return json($res);
        }else{
            $res=[
                "code"=>200,
                "user"=>$user[0]
            ];
            return json($res);
        }
    }
}
